package com.zt.english.views

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.Gravity
import android.view.View
import android.widget.LinearLayout
import com.zt.english.R
import com.zt.english.bean.SubtitlesBean
import com.zt.english.utils.ISubtitlesControl

/**
 * 自定义view
 * 显示字幕的图层
 */
class SubtitlesView(context: Context, attrs: AttributeSet):LinearLayout(context,attrs),ISubtitlesControl, SubtitlesTextView.SubtitleClickListener {
    /**
     * 只显示中文
     */
    val LANGUAGE_TYPE_CHINA = 0

    /**
     * 只显示英文
     */
    val LANGUAGE_TYPE_ENGLISH = LANGUAGE_TYPE_CHINA + 1

    /**
     * 双语显示
     */
    val LANGUAGE_TYPE_BOTH = LANGUAGE_TYPE_ENGLISH + 1
//    /**
//     * 都不显示
//     */
//    val LANGUAGE_TYPE_NONE = LANGUAGE_TYPE_BOTH +1

    /**
     * 字幕所有的数据
     */
    private var data: ArrayList<SubtitlesBean> = ArrayList<SubtitlesBean>()

    /**
     * 中文字幕
     */
    private var subChina: SubtitlesTextView? = null

    /**
     * 英文字幕
     */
    private var subEnglish: SubtitlesTextView? = null

    /**
     * 当前显示节点
     */
    private val subTitleView: View? = null

    /**
     * 单条字幕数据
     */
    private var model: SubtitlesBean? = null

    /**
     * 后台播放
     */
    private var palyOnBackground = false

    /**
     * 当前语言形式类型
     */
    private var language = LANGUAGE_TYPE_BOTH

    init{
        val mView = View.inflate(context, R.layout.layout_subtitles,null)
        subChina = mView.findViewById(R.id.subTitlesChina)
        subEnglish = mView.findViewById(R.id.subTitlesEnglish)
        subChina!!.subtitleClickListener = this
        subEnglish!!.subtitleClickListener = this
        this.orientation = VERTICAL
        this.gravity = Gravity.BOTTOM
        //添加自定义layout到本LinearLayout
        this.addView(mView)
    }

    override fun setItemSubtitlesChina(context: String) {
        subChina!!.text = context
    }

    override fun setItemSubtitlesEnglish(context: String) {
        subEnglish!!.text = context
    }

    override fun seekTo(position: Int) {
        if (!data.isEmpty()){
            model = searchSub(data, position);
            System.out.println("seekTo: " +position + "/" + data.get(data.size - 1).end)
        }
        if (model != null) {
            setItemSubtitlesChina(model!!.contentCh);
            setItemSubtitlesEnglish(model!!.contentEn);
        } else {
            setItemSubtitlesChina("");
            setItemSubtitlesEnglish("");
        }
    }

    override fun setData(list: ArrayList<SubtitlesBean>) {
        this.data.clear()
        this.data = list
    }

    override fun setLanguage(type: Int) {
        when(type){
            LANGUAGE_TYPE_CHINA -> {
                subChina!!.visibility = View.VISIBLE
                subEnglish!!.visibility = View.INVISIBLE
            }
            LANGUAGE_TYPE_ENGLISH -> {
                subChina!!.visibility = View.INVISIBLE
                subEnglish!!.visibility = View.VISIBLE
            }
            LANGUAGE_TYPE_BOTH -> {
                subChina!!.visibility = View.VISIBLE
                subEnglish!!.visibility = View.VISIBLE
            }
//            LANGUAGE_TYPE_NONE -> {
//                subChina!!.visibility = View.INVISIBLE
//                subEnglish!!.visibility = View.INVISIBLE
//            }
        }
    }

    override fun setPause(pause: Boolean) {

    }

    override fun setStart(start: Boolean) {

    }

    override fun setStop(stop: Boolean) {

    }

    override fun setPlayOnBackground(backgroundPlay: Boolean) {
        this.palyOnBackground = backgroundPlay
    }

    override fun clickDown() {
        language++;
        setLanguage(language % 3);
        System.out.println("language: " + language + "  " + language % 3)
    }

    override fun clickUp() {

    }

    override fun onWindowFocusChanged(hasWindowFocus: Boolean) {
        super.onWindowFocusChanged(hasWindowFocus)
        System.out.println("onWindowFocusChanged: " + hasWindowFocus)
    }

    /**
     * 采用二分法去查找当前应该播放的字幕
     *
     * @param list 全部字幕
     * @param key 播放的时间点
     * @return
     */
    fun searchSub(list: ArrayList<SubtitlesBean>, key: Int): SubtitlesBean? {
        var start = 0
        var end: Int = list.size- 1
        while (start <= end) {
            val middle = (start + end) / 2
            if (key < list[middle].start) {
                if (key > list[middle].end) {
                    return list[middle]
                }
                end = middle - 1
            } else if (key > list[middle].end) {
                if (key < list[middle].start) {
                    return list[middle]
                }
                start = middle + 1
            } else if (key >= list[middle].start && key <= list[middle].end) {
                return list[middle]
            }
        }
        return null
    }

}